package com.by.controller.sys;

import com.by.common.bean.ResultBean;
import com.by.controller.BaseController;
import com.by.entity.sys.Menu;
import com.by.service.sys.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/menu")
public class MenuController extends BaseController {

    @Autowired
    MenuService menuService;

    @PostMapping("/save")
    public ResultBean save(@RequestBody Menu menu) {
        int rs = menuService.save(menu);
        return success("");
    }

    @GetMapping("/tree")
    public ResultBean queryMenuTree() {
        List list = menuService.queryMenuTree();
        return success("一二级菜单树", list);
    }


    @GetMapping("/child")
    public ResultBean queryChild(String pid) {
        List list = menuService.queryChild(pid);
        return success("子菜单", list);
    }

    @PutMapping("/update")
    public ResultBean update(@RequestBody Menu menu) {
        int rs = menuService.update(menu);
        return rs == 1 ? success("更新成功") : fail("更新失败");
    }

    @DeleteMapping("/delete")
    public ResultBean delete(String id) {
        int rs = menuService.delete(id);
        if (rs == 0) {
            return success("删除失败");
        } else if (rs == 2) {
            return fail("该菜单下有子菜单，无法删除！");
        }
        return success("删除成功");
    }

    @GetMapping("/auth")
    public ResultBean queryAuths(String roleId) {
        Map map = menuService.queryAuths(roleId);
        return success("权限", map);
    }


}
